package com.iefgames.cs.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Vector;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.mortbay.util.ajax.Continuation;

import com.iefgames.cs.manager.CometManager;
import com.iefgames.cs.shout.MessageShout;
import com.iefgames.cs.shout.Shout;

public class Message extends HttpServlet {
	
	private static Log logger = LogFactory.getLog(Message.class); 
	
	@SuppressWarnings("unchecked")
	public void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		Enumeration<String> names = request.getParameterNames();
		
		String name = null;
		ArrayList<String> params = new ArrayList<String>();
		while (names.hasMoreElements()) {
			params.add((name = names.nextElement()) + "=" + request.getParameter(name));
		}
		
		logger.info("------>" + request.getRequestURI() + " " + params.toString());
		
		int state = 0;
		
		String toUuid = request.getParameter("to");
		String fromUuid = request.getParameter("from");
		String message = request.getParameter("msg");
		
		if (StringUtils.EMPTY.equals(message)
				|| StringUtils.EMPTY.equals(toUuid)
				|| StringUtils.EMPTY.equals(fromUuid)) {
			//消息为空
			return ;
		}
		
		
		Shout shout = new MessageShout();
		shout.message = message;
		shout.toUuid = toUuid;
		shout.fromUuid = fromUuid;
		
		//TODO: 消息暂不处理
//		MessageManager.add(toUuid, shout);
//		MessageManager.add(fromUuid, shout);
		
		
		Vector<Shout> shoutsA = null;
		Continuation continuationA = CometManager.get(toUuid);
		try {
			shoutsA = (Vector<Shout>) continuationA.getObject();
		} catch (NullPointerException ne) {
			state = 601;
		}

		Vector<Shout> shoutsB = null;
		Continuation continuationB = CometManager.get(fromUuid);
		try {
			shoutsB = (Vector<Shout>) continuationB.getObject();
		} catch (NullPointerException ne) {
			state = 602;
		}

		if (state > 600) {
			PrintWriter writer = null;
			try {
				writer = response.getWriter();
				writer.append("<html>");
				writer.append("<head>");
				writer.append("<script type='text/javascript'>");
				writer.append("document.domain='iefgames.com';");
				writer.append("</script>");
				writer.append("</head>");
				writer.append("<body onload=\"javascript:window.parent.Iefgames_CS._('callback', ");
				writer.append("" + state);
				writer.append(")\">");
				writer.append("</script>");
				writer.append("</body>");
				writer.append("<html>");
				writer.flush();
			} catch (IOException ioe) {
			} finally {
				if (writer != null) {
					writer.close();
				}
			}
			
			return;
		}
		
		shoutsA.add(shout);
		continuationA.resume();
		
		shoutsB.add(shout);
		continuationB.resume();
	}
}
